﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ThuVien;
using System.Data;
namespace TranTekBE.News
{
    public partial class Read : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            DataSet newer = FunctionsUtils.Database2.ExecuteQuery("select top 5 * from tbl_News where ID not in (select top 3 ID from tbl_News where slider != '' order by ID desc) order by id desc");
            if (newer != null && newer.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow row in newer.Tables[0].Rows)
                {
                    popular.Text += "<li><a href=\"Read.aspx?id=" + row[0].ToString() + "\">";
                    popular.Text += "<img src=\"Images/avatar/" + row[7].ToString() + "\"></a>";
                    popular.Text += "<h3><a href=\"Read.aspx?id=" + row[0].ToString() + "\">" + row[1].ToString() + "</a></h3>";//comments
                    popular.Text += "<span class=\"comments\">" + row[8].ToString() + "</span>";
                    popular.Text += "<span class=\"post-date\">" + row[5].ToString().Split(' ')[0] + "</span></li>";
                }
            }
            DataSet datarecent = FunctionsUtils.Database2.ExecuteQuery("select top 5 * from tbl_News order by Viewer desc");
            if (datarecent != null && datarecent.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow row in datarecent.Tables[0].Rows)
                {
                    recent.Text += "<li><a href=\"Read.aspx?id=" + row[0].ToString() + "\">";
                    recent.Text += "<img src=\"Images/avatar/" + row[7].ToString() + "\"></a>";
                    recent.Text += "<h3><a href=\"Read.aspx?id=" + row[0].ToString() + "\">" + row[1].ToString() + "</a></h3>";
                    recent.Text += "<span class=\"comments\">" + row[8].ToString() + "</span>";
                    recent.Text += "<span class=\"post-date\">" + row[5].ToString().Split(' ')[0] + "</span></li>";
                }
            }
            string id = Request.QueryString["id"];
            if (id != null)
            {
                DataSet post = FunctionsUtils.Database2.ExecuteQuery("select * from tbl_News where id = "+id);
                if (post != null && post.Tables[0].Rows.Count > 0)
                {
                     foreach (DataRow row in post.Tables[0].Rows)
                    {
                         if(row[6].ToString().Split(' ')[0] != "")
                             imageslide.Text = "<img src=\"Images/slider/" + row[6].ToString() + "\" alt=\"" + row[1].ToString() + "\">";
                         title.Text = row[1].ToString();
                         content.Text = row[2].ToString();
                         timer.Text = row[5].ToString().Split(' ')[0];
                         poster.Text = row[9].ToString();
                         viewer.Text = row[8].ToString();
                         del.Text = "<a href=\"Delete.aspx?id=" + row[0].ToString() + "\">Xóa</a>";
                         string update = "update tbl_News set Viewer = Viewer + 1 WHERE ID= " + id;
                         FunctionsUtils.Database2.ExecuteNonQuery(update);
                    }
                }
                else
                    Response.Redirect("Home.aspx");
            }
            else
            {
                Response.Redirect("Home.aspx");
            }
        }
    }
}